package cn.e4j.modular.api.controller;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.service.FwslService;
import cn.e4j.modular.api.service.Gwat200Service;
import cn.e4j.modular.api.service.WXUserService;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.MmraT;
import cn.e4j.modular.system.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenchuan.li on 2018/04/11.
 */
@RestController
@RequestMapping("api/wx/fwsl")
public class FwslController extends ControllerSupport {


    @Autowired
    private FwslService fwslService;

    @Autowired
    private WXUserService wXUserService;

    @Autowired
    Gwat200Service gwat200Service;

    /**
     * 获取已购设备列表
     *
     * @return
     */
    @RequestMapping(value = "getServiceOrderList")
    public ResponseData getServiceOrderList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        SysUser currentUser = wapper.getSysUser();
        try {

            // 获取区域权限-进行省市过滤数据
            inDto.put("GWAA003", currentUser.getACCOUNT());
            inDto.put("MMRE004S",gwat200Service.getMmreData(inDto));
            inDto.put("MMRDENT", inDto.getString("ENT"));

            list = fwslService.getServiceOrderList(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
           return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 获取已购设备 - 设备详情
     *
     * @return
     */
    @RequestMapping(value = "getOrderDetail")
    public ResponseData getOrderDetail(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<MmraT>  list = new ArrayList<>();
        try {
            inDto.put("MMRAENT", inDto.getString("ENT"));//集团编号
            QueryWrapper<MmraT> mmraTQueryWrapper = new QueryWrapper<>();
            mmraTQueryWrapper.eq("MMRAENT", inDto.getString("MMRAENT"));//集团编号
            mmraTQueryWrapper.eq("MMRA002", inDto.getString("MMRA002"));
            mmraTQueryWrapper.eq("MMRA003", inDto.getString("MMRA003"));
            mmraTQueryWrapper.eq("MMRAACTI", "Y");

            list = new MmraT().selectList(mmraTQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 保存
     *
     * @return
     */
    @RequestMapping(value = "orderSave")
    public ResponseData orderSave(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        try {
            // 需要传入 id,修改需要
            inDto.put("MMRD020", "2");
            inDto.put("MMRD021", currentUser.getID());
            inDto.put("MMRD022", DateUtils.getDate(DateUtils.PATTERN_yyyy_MM_dd));
            inDto.put("MMRD027", DateUtils.getDate(DateUtils.PATTERN_yyyy_MM_dd));
            inDto.put("MMRDMODU", currentUser.getID());
            fwslService.orderSave(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 结案
     *
     * @return
     */
    @RequestMapping(value = "closeCase")
    public ResponseData closeCase(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("MMRD020", "6");
            inDto.put("MMRD023", "0");
            inDto.put("MMRD027", DateUtils.getDate(DateUtils.PATTERN_yyyy_MM_dd));
            inDto.put("MMRDMODU", currentUser.getID());
            fwslService.orderSave(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 派工
     *
     * @return
     */
    @RequestMapping(value = "dispatching")
    public ResponseData dispatching(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        try {
            // 需要传入 id,修改需要
            inDto.put("MMRD020", "3");
            inDto.put("MMRD023", "1");
            inDto.put("MMRD027", DateUtils.getDate(DateUtils.PATTERN_yyyy_MM_dd));
            inDto.put("MMRDMODU", currentUser.getID());
            fwslService.orderSave(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 获取客户对应的维修部
     *
     * @return
     */
    @RequestMapping(value = "getWxbByUserCode")
    public ResponseData getWxbByUserCode(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        try {
            list = wXUserService.getWxbByUserCodeList(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

}
